var express = require('express');
var router = express.Router();
var request = require('request');
var querystringify = require('querystringify');
var manhuaDao = require('../dao/manhuaDao');
var utils = require('../dao/utils');


// img防盗图片的中转处理
router.get('/img', (req, res) => {
    console.log(req.query);


    var imgUrl = encodeURI(req.query.url);

    console.log('-------------');
    console.log(imgUrl);


    // Referer头信息
    var Referer = req.query.Referer;


    var options = {
        url: imgUrl,
        headers: {
            'Referer': Referer?Referer:"http://manhua.dmzj.com/tags/riben.shtml"
        }
    };
    request(options).on('error', function (err) {
        console.log('图片请求戳错');
        // http://www.1kkk.com/vol1-941/
        console.log(err);
        res.json({ success: false, info_message: "图片请求失败" });
    }).pipe(res);

});




// 1kkkmanhua
router.get('/get1kkkImageParam',function(req,res){

    var url = req.query.url;

    request({ url: url, encoding: null, timeout: 8000 }, function (err, response, body) {
        console.log('/get1kkkImageParam');
        // 超时错误处理
        if (err) {
            console.log(err);
            res.json({ success:false,error: err, body: body });
            return;
        }
        
        if(body){
            var html = body.toString('utf-8');
        }else{
            console.log(body);
            res.json({ success:false,error: err, body: body });
        }


        // console.log(html);
        console.log('---x-x-x-');
        console.log(utils.kkkmatchData(html));
        var param = utils.kkkmatchData(html); 
        
        res.json({success:true,data:param});
    });
});
// dmzj
router.get('/getdmzjImages',function(req,res){

    var url = req.query.url;

    request({ url: url, encoding: null, timeout: 80000 }, function (err, response, body) {
        console.log('/getdmzjImages');
        // 超时错误处理
        if (err) {
            console.log(err);
            res.json({ success:false,error: err, body: body });
            return;
        }
        
        if(body){
            var html = body.toString('utf-8');
        }else{
            console.log(body);
            res.json({ success:false,error: err, body: body });
            return;
        }

        // console.log(html);
        console.log('---x-x-x-');
        console.log(utils.dmzjmatchData(html));
        var param = utils.dmzjmatchData(html); 
        
        res.json({success:true,data:param});
    });
});
// 漫画柜
router.get('/getmhgImages',function(req,res){

    
    var url = req.query.url;

    request({ url: url, encoding: null, timeout: 80000,headers:{"Host": "www.manhuagui.com"} }, function (err, response, body) {
        console.log('/getmhgImages');
        // 超时错误处理
        if (err) {
            console.log(err);
            res.json({ success:false,error: err, body: body });
            return;
        }
        
        if(body){
            var html = body.toString('utf-8');
        }else{
            console.log(body);
            res.json({ success:false,error: err, body: body });
            return;
        }


        // console.log(html);
        console.log('---x-x-x-');
        console.log(utils.mhgmatchData(html));


        var param = utils.mhgmatchData(html); 
        
        res.json({success:true,data:param});
    });
});


// jsonp
router.get('/proxy', (req, res) => {

    console.log('----------url-------:/proxy');
    console.log(req.query.url + "?" + querystringify.stringify(JSON.parse(req.query.data)));
    
    console.log(req.query.Referer);
    var proxyUrl = req.query.url;
    var options = {
        url: proxyUrl + "?" + querystringify.stringify(JSON.parse(req.query.data)),
        headers: {
            'Referer': req.query.Referer
        }
    };
    request(options,function(err,response,body){
        if(err){
            res.json({success:false,msg:'转发出错'});
        }else{
            res.json({success:true,data:body});
        }
    });  

});

// sign up
router.get('/search', (req, res) => {

    console.log(req.query);
    // res.json({success:true});

    if (!req.query) {
        res.json({ success: false, info_message: "没有有效参数" });
    } else {

        manhuaDao.search(req.query, res);
    }

});
router.post('/search', (req, res) => {

    console.log(req.body);
    // res.json({success:true});

    if (!req.body) {
        res.json({ success: false, info_message: "没有有效参数" });
    } else {

        manhuaDao.search(req.body, res);
    }

});

// pages
router.get('/page', (req, res) => {

    console.log(req.query);
    // res.json({success:true});

    if (!req.query) {
        res.json({ success: false, info_message: "没有有效参数" });
    } else {

        manhuaDao.page(req.query, res);
    }

});
router.post('/page', (req, res) => {

    console.log(req.body);
    // res.json({success:true});

    if (!req.body) {
        res.json({ success: false, info_message: "没有有效参数" });
    } else {

        manhuaDao.page(req.body, res);
    }

});


// detail
router.get('/detail', (req, res) => {
    if (!req.query) {
        res.json({ success: false, info_message: "没有有效参数" });
    } else {

        manhuaDao.find(req.query, res);
    }
});

router.post('/isUpdate', (req, res) => {

    manhuaDao.isUpdate(req.body, res);

});

module.exports = router;